package com.carlos.company.controller;

import com.carlos.company.pojo.SysUser;
import com.carlos.company.service.SysUserService;
import com.carlos.company.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 李健成
 * @date 2021/10/14
 */

@RestController
@RequestMapping("/user")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @GetMapping("")
    public ResultVO<Object> list(@RequestParam(value = "username", required = false) String username) {
        if (username != null) {
            return new ResultVO<>().success().data(sysUserService.getByUsername(username));
        } else {
            return new ResultVO<>().success().data(sysUserService.list());
        }

    }

//    @PostMapping("/login")
//    public String login() {
//        return "login";
//    }

    @PutMapping("/update")
    public ResultVO<Void> updatePassword(
            @RequestParam("username") String username,
            @RequestParam("password") String password
    ) {
        SysUser user = sysUserService.getByUsername(username);
        user.setPassword(passwordEncoder.encode(password));
        sysUserService.updateById(user);
        return new ResultVO<Void>().success();
    }



}
